<?

/**
 * $Id: SharedController.php 1254 2008-09-09 08:39:51Z lule $
 *
 * Kontroler elementow wspolnych
 *
 */
class ServerController extends MainController implements ControllerInterface {
	
	public function IndexAction($param)
	{
		
	}
	
	public function preDispatch($param)
	{
		
	}
	
	public function postDispatch($param)
	{
		
	}


	public function PhotoCropperAction($param) {
		$this->Run();
		$this->setAjaxRender();

		$type = '';

		if(isset($param['type']))
		{
			$type = $param['type'];
		}
		$this->smarty->assign('type', $type);

		if(Request::IsPost()) {
			$action = Request::GetPost('action');
			switch($action) {
				case 'uploadImage':
					if(!is_uploaded_file($_FILES['image']['tmp_name'])) {
						$this->smarty->assign('error', $this->t('Upload file'));
						break;
					}
					try{
						ob_start();
						$image = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
						ob_get_clean();	
					}catch(Exception $e){
						$this->smarty->assign('error',$this->t('File is incorrect'));
						break;
					}
					
					if($image) {
						$width = imagesx($image);
						$height = imagesy($image);
						if($width < 120 || $height < 120) {
							$this->smarty->assign('error', $this->t('minimum size is 150x150'));
							break;
						}
						
						$filename = 'temp/uploads/'. md5(microtime()).'.jpg';
						
						
						if($width > 400 || $height > 400) {
							$nwidth = 400;
							$nheight = 400;
							if($width / $height > 1) {
								$nheight = intval($height * $nwidth / $width);
							}else{
								$nwidth = intval($width * $nheight / $height);
							}
							
							$newimg = imagecreatetruecolor($nwidth,$nheight);
							imagecopyresampled($newimg,$image,0,0,0,0,$nwidth,$nheight,$width,$height);
							
							imagedestroy($image);
							imagejpeg($newimg,PATH_STATIC . '/' .  $filename,100);
							imagedestroy($newimg);
						}else{
							imagejpeg($image,PATH_STATIC . '/' . $filename,100);
							imagedestroy($image);
						}
						
						$this->smarty->assign('allowCropp',1);
						$this->smarty->assign('imageSrc',$filename);
						
					}else{
						$this->smarty->assign('error',$this->t('incorrect file'));
						break;
					}
					break;
				case 'cropp':
					$coords = Request::getPost('coords');
					$file = Request::getPost('file',false); 
					$fileName = explode('/', $file);
					$fileName = array_pop($fileName);
					
					$cropped = imagecreatetruecolor($coords[2],$coords[3]);
					$src = imagecreatefromstring(file_get_contents(PATH_STATIC . '/' . $file));
					
					imagecopyresampled($cropped,$src,0,0,$coords[0],$coords[1],$coords[2],$coords[3],$coords[2],$coords[3]);
					
					imagedestroy($src);
					imagejpeg($cropped,PATH_STATIC . '/' . $file,100);
					imagedestroy($cropped);
					
					
					
					//tutaj jedynka poniewaĹĽ 1 to taka grupa pusta w ktĂłrej moĹĽna daÄ‡ np domyĹ›lne avatary
					$groupId = 1;
					
					
					
					$imageSrc = $file;
					
					switch($param['type']){
						case 'avatar':
							$imageSrc = '/upload/user/avatar/' . $fileName;
							rename(PATH_STATIC . '/' . $file, PATH_STATIC . $imageSrc);
							$imageSrc2 = URL_STATIC_CONTENT . $imageSrc;
							setcookie('avatarTemp',$imageSrc2,time() + 60);
							setcookie('imgGroup',$groupId,time() + 60);
							if(isset($param['action']) && $param['action'] == 'save' && $this->userLoggedIn) {
								$this->user->setAvatar($imageSrc);
								UserDAL::Update(new DalData(array('obj' => $this->user)));
							}
							break;

						case 'logo2':
							list($groupId, $imageSrc) = ImageDAL::AvatarSave($file);
							$imageSrc2 = URL_STATIC_CONTENT2 . '/upload/image/avatar/' . $imageSrc;

							setcookie('logoTemp',$imageSrc,time() + 60);
							setcookie('imgGroupCompany',$groupId,time() + 60);

							if(isset($param['action']) && $param['action'] == 'save' && $this->userLoggedIn) {
								$this->user->setIdImageGroupCompany($groupId);
								$this->user->setLogo('NULL');
								UserDAL::Update(new DalData(array('obj' => $this->user)));
							}

							break;

						default:
					}
					
					
					$this->smarty->assign('cropped',1);
					$this->smarty->assign('groupId',$groupId);
					$this->smarty->assign('imageSrc',$imageSrc);
					break;
				default;
			}
		}
	}	
	
	/**
	 * Wyswietlanie obrazka captcha
	 */
	public function CaptchaAction() {
		//$this->disableTemplates = true;
		$this->SetAjaxRender();
		include(PATH_CORE.'/plugins/Captcha.php');
		$word = rand(2,9).rand(2,9).rand(2,9).rand(2,9).rand(2,9);

		setcookie(CAPTCHA_COOKIE_NAME, (md5(strtolower($word) . CAPTCHA_SEED)));
		//session_start();
		//$_SESSION['captcha'] = $word;
		$image = confirm($word);
		$this->content = $image;
		//die();
	}
	
}

?>